
package com.fire.web.modules.all.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.*;
import com.fire.web.modules.all.mapper.AllUserMapper;
import com.fire.web.modules.all.mapper.FireMaintainUserMapper;
import com.fire.web.modules.all.mapper.FireMaintainorMapper;
import com.fire.web.modules.all.mapper.UserAuthorityMapper;
import com.fire.web.modules.all.request.AddUserReq;
import com.fire.web.modules.all.service.IFireMaintainUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 维保单位用户Service
 *
 * @author Matthew
 * @Date 2019-01-10 10:42
 */
@Service
public class FireMaintainUserServiceImpl extends ServiceImpl<FireMaintainUserMapper, FireMaintainUserModel> implements IFireMaintainUserService {

	private static final Logger LOGGER = LoggerFactory.getLogger(FireMaintainUserServiceImpl.class);

	@Autowired
	private FireMaintainUserMapper fireMaintainUserMapper;
	@Autowired
	private FireMaintainorMapper fireMaintainorMapper;
	@Autowired
	private AllUserMapper allUserMapper;
	@Autowired
	private UserAuthorityMapper userAuthorityMapper;

	@Override
	public FireMaintainUserModel selectOne(FireMaintainUserModel entity) {
		return fireMaintainUserMapper.selectOne(entity);
	}

	@Override
	public List<FireMaintainUserModel> selectList(FireMaintainUserModel model) {
		return fireMaintainUserMapper.selectList(model, new EntityWrapper<FireMaintainUserModel>());
	}

	@Override
	public List<FireMaintainUserModel> selectList(FireMaintainUserModel model, Wrapper<FireMaintainUserModel> wrapper) {
		return fireMaintainUserMapper.selectList(model, wrapper);
	}

	@Override
	public List<FireMaintainUserModel> selectPage(Pagination pagination, FireMaintainUserModel model, Wrapper<FireMaintainUserModel> wrapper) {
		return fireMaintainUserMapper.selectPage(pagination, model, wrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult addMatUser(AddUserReq req, String adminid) {
		//先添加t_mit_user获得userid
		AllUserModel allUserModel = new AllUserModel();
		allUserModel.setAliasname(req.getUsername());
		Integer integer = allUserMapper.selectCount(new EntityWrapper<AllUserModel>().eq("phone", req.getPhone()));
		if(integer.intValue()>0){
			return new AjaxResult(ErrorCodeConstants.PHONE_ERROR,"电话号码重复","电话号码重复");
		}
		allUserModel.setPhone(req.getPhone());
		allUserModel.setCreatetime(new Date());
		allUserModel.setUpdatetime(allUserModel.getUpdatetime());
		allUserModel.setChannelid(1);
		allUserModel.setChannelname("fire");
		allUserMapper.insert(allUserModel);
		AllUserModel select = new AllUserModel();
		select.setPhone(req.getPhone());
		allUserModel = allUserMapper.selectOne(select);

		//根据adminid获取维保单位id
		FireMaintainorModel fireMaintainorModel = new FireMaintainorModel();
		fireMaintainorModel.setAdminid(adminid);
		fireMaintainorModel = fireMaintainorMapper.selectOne(fireMaintainorModel);

		//插入维保单位用户
		FireMaintainUserModel fireMaintainUserModel = new FireMaintainUserModel();
		fireMaintainUserModel.setUsername(req.getUsername());
		fireMaintainUserModel.setPhone(req.getPhone());
		fireMaintainUserModel.setImageurl(req.getImageurl());
		fireMaintainUserModel.setIdentification(req.getIdentification());
		fireMaintainUserModel.setCreatetime(new Date());
		fireMaintainUserModel.setUpdatetime(fireMaintainUserModel.getCreatetime());
		fireMaintainUserModel.setAdminid(adminid);
		fireMaintainUserModel.setMaintainor(fireMaintainorModel.getMaintainor());
		fireMaintainUserModel.setUserid(allUserModel.getUserid());
		fireMaintainUserMapper.insert(fireMaintainUserModel);
		//插入业主权限
		UserAuthorityModel userAuthorityModel = new UserAuthorityModel();
		userAuthorityModel.setUserid(allUserModel.getUserid());
		userAuthorityModel.setAuthority("maintainor");
		userAuthorityModel.setIsmodifyaccet(req.getIsmodifyaccet());
		userAuthorityModel.setIsmodifydevice(req.getIsmodifydevice());
		userAuthorityModel.setIsmodifysite(req.getIsmodifysite());
		userAuthorityModel.setCreatetime(new Date());
		userAuthorityModel.setUpdatetime(userAuthorityModel.getCreatetime());
		userAuthorityMapper.insert(userAuthorityModel);
		return new AjaxResult(ErrorCodeConstants.SUCCESS,"新增用户成功","新增用户成功");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult updateMatUser(AddUserReq req) {
		//先更新t_mit_user
		AllUserModel allUserModel = new AllUserModel();
		allUserModel.setUserid(req.getUserid());
		allUserModel.setAliasname(req.getUsername());
		//查询电话是否重复
		Integer integer = allUserMapper.selectCount(new EntityWrapper<AllUserModel>().ne("userid", req.getUserid()).eq("phone", req.getPhone()));
		if(integer.intValue()>0){
			return new AjaxResult(ErrorCodeConstants.PHONE_ERROR,"电话号码重复","电话号码重复");
		}
		allUserModel.setPhone(req.getPhone());
		allUserModel.setUpdatetime(new Date());
		allUserMapper.updateById(allUserModel);

		//再更新维保用户表
		FireMaintainUserModel fireMaintainUserModel = new FireMaintainUserModel();
		fireMaintainUserModel.setUserid(req.getUserid());
		fireMaintainUserModel.setPhone(req.getPhone());
		fireMaintainUserModel.setIdentification(req.getIdentification());
		fireMaintainUserModel.setUsername(req.getUsername());
		fireMaintainUserModel.setImageurl(req.getImageurl());
		fireMaintainUserModel.setUpdatetime(new Date());
		fireMaintainUserMapper.updateById(fireMaintainUserModel);

		//插入业主权限
		UserAuthorityModel userAuthorityModel = new UserAuthorityModel();
		userAuthorityModel.setUserid(allUserModel.getUserid());
		userAuthorityModel.setIsmodifyaccet(req.getIsmodifyaccet());
		userAuthorityModel.setIsmodifydevice(req.getIsmodifydevice());
		userAuthorityModel.setIsmodifysite(req.getIsmodifysite());
		userAuthorityModel.setUpdatetime(new Date());
		userAuthorityMapper.updateById(userAuthorityModel);

		return new AjaxResult(ErrorCodeConstants.SUCCESS,"更新用户成功","更新用户成功");
	}

}
